package org.example.controller.user;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.common.result.Result;
import org.example.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 企业用户端短信服务控制器
 */
@RestController
@RequestMapping("/user/sms")
@Slf4j
@Component("userSmsController")
@Tag(name = "用户端-短信验证码", description = "用户端短信验证码相关接口")
public class SmsController {

    @Autowired
    private SmsService smsService;

    /**
     * 用户端发送登录验证码（支持 JSON 或 表单）
     */
    @PostMapping("/send")
    @io.swagger.v3.oas.annotations.Operation(
        summary = "用户端-发送登录验证码",
        description = "为用户端用户发送登录验证码"
    )
    public Result<Boolean> sendLoginCode(@RequestBody(required = false) Map<String, String> body,
                                         @RequestParam(value = "phone", required = false) String phone) {
        try {
            if (phone == null && body != null) {
                phone = body.get("phone");
            }
            if (phone == null || phone.trim().isEmpty()) {
                return Result.badRequest("手机号不能为空");
            }
            boolean success = smsService.sendLoginCode(phone, "user");
            return success ? Result.success(true) : Result.error("验证码发送失败");
        } catch (Exception e) {
            log.error("用户端发送验证码失败: {}", phone, e);
            return Result.error("验证码发送失败");
        }
    }

    /**
     * 用户端发送自定义验证码（支持 JSON 或 表单）
     */
    @PostMapping("/send/code")
    public Result<Boolean> sendCode(@RequestBody(required = false) Map<String, String> body,
                                    @RequestParam(value = "phone", required = false) String phone,
                                    @RequestParam(value = "code", required = false) String code) {
        try {
            if ((phone == null || code == null) && body != null) {
                if (phone == null) phone = body.get("phone");
                if (code == null) code = body.get("code");
            }
            if (phone == null || phone.trim().isEmpty() || code == null || code.trim().isEmpty()) {
                return Result.badRequest("手机号和验证码不能为空");
            }
            boolean success = smsService.sendVerificationCode(phone, code, "user");
            return success ? Result.success(true) : Result.error("验证码发送失败");
        } catch (Exception e) {
            log.error("用户端发送验证码失败: {} -> {}", phone, code, e);
            return Result.error("验证码发送失败");
        }
    }

    /**
     * 用户端发送修改手机号验证码（新手机号无需已注册）
     */
    @PostMapping("/send/change-phone")
    @io.swagger.v3.oas.annotations.Operation(
        summary = "用户端-发送修改手机号验证码",
        description = "为新手机号发送验证码，用于修改手机号场景，无需已注册"
    )
    public Result<Boolean> sendChangePhoneCode(@RequestBody(required = false) Map<String, String> body,
                                              @RequestParam(value = "phone", required = false) String phone) {
        try {
            if (phone == null && body != null) {
                phone = body.get("phone");
            }
            if (phone == null || phone.trim().isEmpty()) {
                return Result.badRequest("手机号不能为空");
            }
            // 修改手机号场景：新手机号无需已注册，直接发送验证码
            boolean success = smsService.sendLoginCode(phone, "user");
            return success ? Result.success(true) : Result.error("验证码发送失败");
        } catch (Exception e) {
            log.error("用户端发送修改手机号验证码失败: {}", phone, e);
            return Result.error("验证码发送失败");
        }
    }
}

